﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Jitter.LinearMath;

namespace SunburnJitter.Primitives3D
{
    public class ConvexHullPrimitive : GeometricPrimitive
    {

        public JConvexHull ConvexHull = new JConvexHull();

        public ConvexHullPrimitive(GraphicsDevice device, List<JVector> pointCloud)
        {
            ConvexHull.Build(pointCloud);

            int counter = 0;

            foreach (JConvexHull.Face face in ConvexHull.HullFaces)
            {
                this.AddVertex(pointCloud[face.VertexC].ToVector3(), face.Normal.ToVector3());
                this.AddVertex(pointCloud[face.VertexB].ToVector3(), face.Normal.ToVector3());
                this.AddVertex(pointCloud[face.VertexA].ToVector3(), face.Normal.ToVector3());

                this.AddIndex(counter + 0);
                this.AddIndex(counter + 1);
                this.AddIndex(counter + 2);

                counter+=3;
            }
            

            this.InitializePrimitive(device);
        }

    }
}
